import { Controller } from "../../mvc/core/Controller";
import { ITreasureController } from "./interfaces/ITreasureController";
import { IView } from "../../mvc/interfaces/IView";
import { TreasureModule } from "../../../modules/TreasureModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { GameController } from "../mvc/GameController";
import { ELottertType } from "../../../Message/Public/ELottertType";
import { ExplorerRumorData } from "../view/treasure/ExplorerRumorData";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ECareer } from "../../../Message/Public/ECareer";
import { SystemSetter } from "../view/system/SystemSetter";
import { SMoney } from "../../../Message/Game/SMoney";
import { SPublicNotice } from "../../../Message/Public/SPublicNotice";
import { EShowArea } from "../../../Message/Public/EShowArea";
import { TreasureListWindow } from "../view/treasure/TreasureListWindow";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SLotteryCopyStatus } from "../../../Message/Public/SLotteryCopyStatus";
import { SceneConfig } from "../resource/SceneConfig";
import { NPCConfig } from "../resource/NPCConfig";
import { STreasureInfo } from "../../../Message/Public/STreasureInfo";
import { FairylandTreasureData } from "../view/copy/FairylandTreasureData";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { ItemConfig } from "../resource/ItemConfig";
import { ImageInfo } from "../../../com/gengine/resource/info/ImageInfo";
import { Global } from "../../../com/gengine/global/Global";
import { LayerManager } from "../manager/LayerManager";
import { TweenLite } from "../../../com/greensock/TweenLite";
import { ITreasureModule } from "../../../modules/interfaces/ITreasureModule";
import { FairylandItemShowPanel } from "../view/copy/FairylandItemShowPanel";
import { CopyFairylandPropPanel } from "../view/copy/CopyFairylandPropPanel";
type int = number;
//class TreasureController
    
    export  class TreasureController extends Controller implements ITreasureController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._treasureModule == null) 
            {
                this._treasureModule = new TreasureModule();
            }
            return this._treasureModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.TreasureExplorer, this.onTreasureExplorerHandler);
            Dispatcher.addEventListener(EventName.TreasureGetExplorerRecords, this.onGetExplorerRecordsHandler);
            Dispatcher.addEventListener(EventName.TreasureModuleOpen, this.onTreasureModuleOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasurePackChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasureGetItemsChangeHandler);
            Dispatcher.addEventListener(EventName.TreasureGetRecordsSuccess, this.onGetRecordsSuccessHandler);
            Dispatcher.addEventListener(EventName.TreasureExplorerRumorAdd, this.onExplorerRumorAddHandler);
            Dispatcher.addEventListener(EventName.TreasureListWindow, this.onTreasureListWindowHandler);
            Dispatcher.addEventListener(EventName.TreasureUseProp, this.onFairylandUsePropHandler);
            Dispatcher.addEventListener(EventName.TreasureBuyAndUseProp, this.onTreasureBuyAndUsePropHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLotteryCopyStatus, this.onLotteryCopyStatusHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicOnDropTreasureItem, this.onDropTreasureItemHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackItemsChangeHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            return;
        }

        /* internal  */onTreasureExplorerHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.sceneProxy.explorer(loc1.type, loc1.amount, loc1.useItem);
            if (!GameController.treasureWarehourse.treasureGetItem.isHide) 
            {
                GameController.treasureWarehourse.treasureGetItem.hide();
            }
            if (loc1.type == ELottertType._ELotterySeabed || loc1.type == ELottertType._ELotteryHeaven || loc1.type == ELottertType._ELotteryKunlun) 
            {
                this._isExplorer = true;
            }
            return;
        }

        /* internal  */onGetExplorerRecordsHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (ExplorerRumorData.getNoticeArray(loc1) == null) 
            {
                GameProxy.sceneProxy.getExplorerRecords(loc1);
            }
            return;
        }

        /* internal  */onTreasureModuleOpenHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            if (arg1.data != null) 
            {
                if (typeof arg1.data === "itemdata") 
                {
                    loc2 = arg1.data as ItemData;
                    if (ItemsUtil.isLotteryKey(loc2)) 
                    {
                        loc1 = ELottertType._ELotterySeabed;
                    }
                    else if (ItemsUtil.isLotteryHeavenKey(loc2)) 
                    {
                        loc1 = ELottertType._ELotteryHeaven;
                    }
                    else if (ItemsUtil.isLotteryKunlunKey(loc2)) 
                    {
                        loc1 = ELottertType._ELotteryKunlun;
                    }
                }
                else 
                {
                    loc1 = arg1.data as int;
                }
                if (loc1 != ELottertType._ELotterySeabed) 
                {
                    if (cache.role.entityInfo.level < 55 && loc1 == ELottertType._ELotteryHeaven) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35022));
                        return;
                    }
                    if (cache.role.entityInfo.level < 100 && loc1 == ELottertType._ELotteryKunlun) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35022));
                        return;
                    }
                }
                else 
                {
                    loc3 = cache.role.entityInfo.career;
                    if (cache.role.entityInfo.level < 10) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35020));
                        return;
                    }
                    if (loc3 == ECareer._ECareerNo || loc3 == ECareer._ECareerNewPlayer) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35021));
                        return;
                    }
                }
                if (view.isHide) 
                {
                    view.show();
                }
                this._treasureModule.typeChangeUpdate(loc1);
                this.judeShowGuideArrow();
            }
            else if (view.isHide) 
            {
                view.show();
                this.judeShowGuideArrow();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */judeShowGuideArrow(): void
        {
            if (cache.pack.treasurePackCache.getItemsByJudge().length > 0) 
            {
                if (!SystemSetter.localSetter.isGuideOpen) 
                {
                    this._treasureModule.showGuideArrow();
                }
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SMoney;
            if (!(this._treasureModule == null) && !this._treasureModule.isHide) 
            {
                this._treasureModule.updateMoney();
            }
            return;
        }

        /* internal  */onTreasureGetItemsChangeHandler(arg1: Object): void
        {
            var loc1=arg1;
            if (!(this._treasureModule == null) && !this._treasureModule.isHide && this._isExplorer) 
            {
                this._isExplorer = false;
                this._treasureModule.update(loc1);
                this.judeShowGuideArrow();
            }
            return;
        }

        /* internal  */onTreasurePackChangeHandler(arg1: Object): void
        {
            if (!(this._treasureModule == null) && !this._treasureModule.isHide) 
            {
                this._treasureModule.updateCapacity();
            }
            return;
        }

        /* internal  */onGetRecordsSuccessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data["type"] as int;
            if (loc1 == ELottertType._ELotterySeabed || loc1 == ELottertType._ELotteryHeaven || loc1 == ELottertType._ELotteryKunlun) 
            {
                loc2 = arg1.data["records"] as Array<any>;
                ExplorerRumorData.noticeMap[loc1] = loc2;
                if (!(this._treasureModule == null) && !this._treasureModule.isHide) 
                {
                    this._treasureModule.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onExplorerRumorAddHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPublicNotice;
            if (loc1.area & EShowArea._EShowAreaExplorer && (loc1.code == ELottertType._ELotterySeabed || loc1.code == ELottertType._ELotteryHeaven || loc1.code == ELottertType._ELotteryKunlun)) 
            {
                ExplorerRumorData.setNoticeMap([loc1], loc1.code);
                if (!(this._treasureModule == null) && !this._treasureModule.isHide) 
                {
                    this._treasureModule.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onTreasureListWindowHandler(arg1: DataEvent): void
        {
            if (this._treasureList == null) 
            {
                this._treasureList = new TreasureListWindow();
            }
            if (this._treasureList.isHide) 
            {
                this._treasureList.show();
                this._treasureList.updateListTabBar();
            }
            else 
            {
                this._treasureList.hide();
            }
            return;
        }

        /* internal  */onFairylandUsePropHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=1;
            if (typeof arg1.data === "itemdata") 
            {
                loc1 = arg1.data as ItemData;
            }
            else 
            {
                loc1 = arg1.data["itemData"] as ItemData;
                loc2 = arg1.data["amount"] as int;
            }
            if (loc1 != null) 
            {
                GameProxy.packProxy.useLottery(loc1.data.uid, loc2);
            }
            if (arg1.isResult) 
            {
                this._isExplorer = true;
            }
            return;
        }

        /* internal  */onTreasureBuyAndUsePropHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.shopCode;
            var loc3=loc1.itemCode;
            GameProxy.shopProxy.buyItemAndUse(0, loc2, loc3, 1, 1);
            return;
        }

        /* internal  */onLotteryCopyStatusHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLotteryCopyStatus;
            this.showFairyLand(loc1.isOpen);
            return;
        }

        public showFairyLand(arg1: Boolean): void
        {
            cache.copy.isFairylandOpen = arg1;
            if (cache.mainUINavbar.isUIByLevel) 
            {
                NetDispatcher.dispatchCmd(ServerCommand.UIByLevelFairyOpenedChange, {});
            }
            else 
            {
                GameController.gameui.getGameui().resetNavbar();
            }
            SceneConfig.instance.setNpcIsAddStage(NPCConfig.FairyLandNpcId, arg1);
            return;
        }

        /* internal  */onDropTreasureItemHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as STreasureInfo;
            FairylandTreasureData.addTreasureInfo(loc1);
            if (this._fairylandItemShowPanel == null) 
            {
                this._fairylandItemShowPanel = GameController.copy.fairyLandCopy.fairylandItemShowPanel;
            }
            var loc2=this.getAwardImgPath(loc1.itemCode);
            LoaderManager.instance.load(loc2, this.onFlyImgLoadedHandler);
            if (this._fairylandPropPanel == null) 
            {
                this._fairylandPropPanel = GameController.copy.fairyLandCopy.fairylandPropPanel;
            }
            if (!SystemSetter.localSetter.isGuideUse) 
            {
                this._fairylandPropPanel.removeGuideArrow();
            }
            return;
        }

        /* internal  */onBackPackItemsChangeHandler(arg1: Object): void
        {
            if (this._fairylandPropPanel == null && cache.copy.isFairylandOpen) 
            {
                this._fairylandPropPanel = GameController.copy.fairyLandCopy.fairylandPropPanel;
            }
            if (this._fairylandPropPanel && !this._fairylandPropPanel.isHide) 
            {
                this._fairylandPropPanel.updatePropNum();
            }
            if (this._treasureModule && !this._treasureModule.isHide) 
            {
                this._treasureModule.updatePropNum();
            }
            return;
        }

        /* internal  */getAwardImgPath(arg1: int): string
        {
            var loc1=ItemConfig.instance.getInfoByCode(arg1).item.icon;
            return loc1 + ".jpg";
        }

        /* internal  */onFlyImgLoadedHandler(arg1: ImageInfo): void
        {
            var loc1=null;
            loc1 = new Object /* flash.display.Bitmap */(arg1.bitmapData);
            loc1.x = Global.stage.stageWidth / 2 + 100 * Math.random();
            loc1.y = Global.stage.stageHeight / 2 - 100 * Math.random();
            LayerManager.topLayer.addChild(loc1);
            TweenLite.to(loc1, 2, {"x": 260, "y": 350, "width": 32, "height": 32, "onComplete": this.onFlyTreasureEnd, "onCompleteParams":[loc1]});
            return;
        }

        /* internal  */onFlyTreasureEnd(arg1: Object /* flash.display.Bitmap */): void
        {
            if (arg1 && arg1.parent) 
            {
                arg1.parent.removeChild(arg1);
                arg1 = null;
            }
            this._fairylandItemShowPanel.update(FairylandTreasureData.treasureInfos);
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (this._treasureModule && !this._treasureModule.isHide) 
            {
                this._treasureModule.updateTabBar();
            }
            if (this._treasureList && !this._treasureList.isHide) 
            {
                this._treasureList.updateListTabBar();
            }
            return;
        }

        private /* var */_treasureModule: ITreasureModule;

        private /* var */_isExplorer: Boolean=false;

        private /* var */_treasureList: TreasureListWindow;

        private /* var */_fairylandItemShowPanel: FairylandItemShowPanel;

        private /* var */_fairylandPropPanel: CopyFairylandPropPanel;
    }
